﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlTypes;
using System.Data.Sql;
using System.Data.SqlClient;

namespace WindowsFormsApplication1
{
    public partial class Return : Form
    {
        Boolean dno = false;
        Boolean rno = false;

        public Return()
        {
            InitializeComponent();
        }

        private void textBox2_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            new Midval().setDisDate(dtRdate.Value.ToString("yyyy-MM-dd"));
            new dispatch_popup().ShowDialog();
            String val = new Midval().getDisCode();
            val = val.Substring(0, val.IndexOf(" - "));
            txtdNo.Text = val;
        }

        private void txtdNo_TextChanged(object sender, EventArgs e)
        {
            try
            {
                try
                {
                    String getMax = "SELECT ISNULL(MAX(recno),0) FROM returnx";
                    SqlDataReader mmdr = DbConnet.ReadTable(getMax);
                    String mx = "";

                    if (mmdr.Read())
                    {
                        mx = mmdr[0].ToString();
                    }
                    mmdr.Close();

                    //mx = mx.Substring(mx.IndexOf("DI") + 1);

                    int a = (int.Parse(mx) + 1);

                    txtrexno.Text = a.ToString();
                }
                catch
                {
                }


                String getDis = "SELECT Recno,item,noofitems FROM  dispatch WHERE Recno = '" + txtdNo.Text.Trim() + "' AND datex BETWEEN '" + dtRdate.Value.ToString("yyyy-MM-dd") + "' AND '" + dtRdate.Value.AddDays(1).ToString("yyyy-MM-dd") + "'";
                SqlDataReader disdr = DbConnet.ReadTable(getDis);
                String item = "";
                while (disdr.Read())
                {
                    item = disdr["item"].ToString();
                    txtNo.Text = disdr["noofitems"].ToString();
                }
                disdr.Close();

                String getName = "SELECT itemname FROM Items WHERE Itemcode = '" + item + "'";
                SqlDataReader ndr = DbConnet.ReadTable(getName);
                if (ndr.Read())
                {
                    txtItem.Text = item + " - " + ndr["itemname"].ToString();
                }
                ndr.Close();
            }
            catch (Exception exr)
            {
            }

        }

        private void Return_Load(object sender, EventArgs e)
        {
            DbConnet.connect(new Log().getConstr());
        }

        private void txtrexno_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            new Midval().setDisDate(dtRdate.Value.ToString("yyyy-MM-dd"));
            new Return_popup().ShowDialog();
            String val = new Midval().getRecCode();
            val = val.Substring(0, val.IndexOf(" - "));
            txtrexno.Text = val;
        }

        private void txtrexno_TextChanged(object sender, EventArgs e)
        {
            try
            {
                String getd = "SELECT Recno,dispatch,item,noofitems FROM returnx WHERE Recno = '" + txtrexno.Text.Trim() + "' AND datex BETWEEN '" + dtRdate.Value.ToString("yyyy-MM-dd") + "' AND '" + dtRdate.Value.AddDays(1).ToString("yyyy-MM-dd") + "'";
                SqlDataReader ddr = DbConnet.ReadTable(getd);
                String itm = "";
                while (ddr.Read())
                {
                    txtrexno.Text = ddr["Recno"].ToString();
                    txtdNo.Text = ddr["dispatch"].ToString();
                    txtrCount.Text = ddr["noofitems"].ToString();
                    itm = ddr["item"].ToString();
                }
                ddr.Close();

                String getName = "SELECT itemname FROM Items WHERE Itemcode = '" + itm + "'";
                SqlDataReader ndr = DbConnet.ReadTable(getName);
                if (ndr.Read())
                {
                    txtItem.Text = itm + " - " + ndr["itemname"].ToString();
                }
                ndr.Close();
            }
            catch (Exception exrlo)
            {
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                Boolean upd = false;
                Boolean ins = false;
                String itm = txtItem.Text.Trim();
                itm = itm.Substring(0, itm.IndexOf(" - "));

                if (txtrCount.Text.Length <= 0)
                {
                    MessageBox.Show(new Form() { TopMost = true }, "Please Enter Return Value if None Please enter \"0\" ", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    String Updrt = "UPDATE returnx SET noofitems = '" + txtrCount.Text.Trim() + "',updatedby = '" + new Midval().getUser() + "',updateddate = '" + DateTime.Now + "' WHERE Recno = '"+txtrexno.Text.Trim()+"'";
                    string insrt = "INSERT INTO returnx  (Recno,dispatch,datex,item,noofitems,createdby,createddate) " +
                                   " VALUES ('" + txtrexno.Text.Trim() + "','" + txtdNo.Text.Trim() + "','" + dtRdate.Value + "','" + itm + "','" + txtrCount.Text + "','" + new Midval().getUser() + "','" + DateTime.Now + "')";

                    upd = DbConnet.AddEditDel(Updrt);

                    if (!upd)
                        ins = DbConnet.AddEditDel(insrt);

                    if (upd || ins)
                    {
                        int dc = 0;
                        int rc = 0;

                        try
                        {
                            dc = int.Parse(txtNo.Text.Trim());
                        }
                        catch
                        {
                        }

                        try
                        {
                            rc = int.Parse(txtrCount.Text.Trim());
                        }
                        catch
                        {
                        }

                        int inhnd = 0;
                        String getC = "SELECT  ISNULL(noofitems,0) FROM stock WHERE Itemcode = '" + itm + "'";
                        SqlDataReader cdr = DbConnet.ReadTable(getC);

                        if(cdr.Read())
                        {
                            inhnd = int.Parse(cdr[0].ToString());
                        }
                        cdr.Close();

                        String updStk = "UPDATE stock SET noofitems ='" + (inhnd+rc) + "',lastupdateted = '" + DateTime.Now + "' WHERE Itemcode = '" + itm + "'";
                        Boolean upst = DbConnet.AddEditDel(updStk);

                        if (upst)
                        {
                            MessageBox.Show(new Form() { TopMost = true }, "Return Details Added Update Successfully ", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            txtdNo.Clear();
                            txtrexno.Clear();
                            txtItem.Clear();
                            txtrCount.Clear();
                            txtNo.Clear();
                        }
                    }
                }
            }
            catch (Exception exrsav8e)
            {
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Hide();
        }
    }
}
